package com.kaola.kaistory;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.kaola.kaistory.bean.Test;
import com.kaola.kaistory.core.msg.IObserverBase;
import com.kaola.kaistory.core.msg.MessageID;
import com.kaola.kaistory.core.msg.MessageManager;
import com.kaola.kaistory.core.msg.MessageManager.Caller;
import com.kaola.kaistory.core.msg.ob.IAppObserver;
import com.kaola.kaistory.core.msg.ob.imp.AppObserver;
import com.kaola.kaistory.mod.db.DbMgrImp;
import com.kaola.kaistory.utils.LogMgr;

public class MainActivity extends Activity implements OnClickListener{

	private IObserverBase ob = new AppObserver(){
		@Override
		public void IAppObserver_OnForground() {
			LogMgr.d("MainActivity", "IAppObserver_OnForground");
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_main);
		SQLiteDatabase db = openOrCreateDatabase("test.db", MODE_PRIVATE, null);
		DbMgrImp dbMgrImp = new DbMgrImp(db);
		dbMgrImp.create(Test.class);
//		findViewById(R.id.text).setOnClickListener(this);
		MessageManager.getInstance().attachMessage(MessageID.OBSERVER_APP, ob );
	}
	
	@Override
	protected void onDestroy() {
		MessageManager.getInstance().detachMessage(MessageID.OBSERVER_APP, ob );
	}
	@Override
	public void onClick(View view){
		LogMgr.d("MainActivity", "onClick");
		MessageManager.getInstance().asyncNotify(MessageID.OBSERVER_APP,new Caller<IAppObserver>() {

			@Override
			public void call() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ob.IAppObserver_OnForground();
			}
		});
		LogMgr.d("MainActivity", "sendMessage");
	}
}
